package the.broccoli.uc.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.web.APIResult;
import the.broccoli.ac.constant.ResType;
import the.broccoli.ac.dto.ResCondition;
import the.broccoli.ac.dto.ResTree;
import the.broccoli.ac.entity.Res;
import the.broccoli.ac.service.IResService;
import the.broccoli.auth.UserContext;
import the.broccoli.uc.vo.ActionVO;
import the.broccoli.uc.service.IRoleResService;
import the.broccoli.uc.service.IRoleService;
import the.broccoli.uc.service.ITenantAppService;
import the.broccoli.uc.service.IUserRoleService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会话用户权限控制器
 *
 * @author lei.liu
 * @since 2020-04-01 18:20:22
 */
@RestController
@RequestMapping("/auth")
public class AuthResController {

    @Resource
    private IUserRoleService userRoleService;

    @Resource
    private IResService resService;

    @Resource
    private IRoleService roleService;

    @Resource
    private ITenantAppService tenantAppService;

    @Resource
    private IRoleResService roleResService;

    @GetMapping("/session-user/res")
    public APIResult<List<ResTree>> getResForSessionUser() {
        List<ResTree> resTree = new ArrayList<>();
        List<String> roleList = userRoleService.getRoleIdList(UserContext.userId());
        if (!CollectionUtils.isEmpty(roleList)) {
            if (roleService.isAdmin(roleList)) {
                resTree = resService.getTreeForAppId(tenantAppService.getAppIdList(UserContext.tenantId()), Arrays.asList(ResType.DIR, ResType.MENU));
            } else {
                resTree = roleResService.getResTree(roleList, Arrays.asList(ResType.DIR, ResType.MENU));
            }
        }

        return APIResult.success(resTree);
    }

    @GetMapping("/session-user/action")
    public APIResult<List<ActionVO>> getAction(
            @ApiParam(name = "url", value = "url", required = true)
            @RequestParam(value = "url", required = true) String url
    ) {
        List<ActionVO> data = new ArrayList<>();
        // 页面的Action
        List<Res> actionList = getActionList(getResId(url));

        if (!CollectionUtils.isEmpty(actionList)) {
            // 当前用户的角色
            List<String> roleList = userRoleService.getRoleIdList(UserContext.userId());
            // 角色关联的Action
            List<Res> roleActionList = roleResService.getResList(roleList, ResType.ACTION);

            List<String> roleActionResIdList = new ArrayList<>(0);
            if (!CollectionUtils.isEmpty(roleActionList)) {
                roleActionResIdList = roleActionList.stream().map(Res::getResId).collect(Collectors.toList());
            }
            for (Res aRes : actionList) {
                boolean auth = false;
                if (roleActionResIdList.contains(aRes.getResId())) {
                    auth = true;
                }
                data.add(new ActionVO(auth, aRes));
            }
        }
        return APIResult.success(data);
    }

    private String getResId(String url) {
        ResCondition condition = new ResCondition();
        condition.setUrl(url);
        List<Res> resList = resService.listByCondition(condition);
        return CollectionUtils.isEmpty(resList) ? null : resList.get(0).getResId();
    }

    private List<Res> getActionList(String parentId) {
        if (!StringUtils.hasText(parentId)) {
            return null;
        }

        ResCondition condition = new ResCondition();
        condition.setParentId(parentId);
        condition.setResTypeList(Arrays.asList(ResType.ACTION.getValue()));
        return resService.listByCondition(condition);
    }
}
